package com.autonavi.minimap.util;

import android.graphics.*;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

/**
 * Created with IntelliJ IDEA.
 * User: ning.dai
 * Date: 13-5-20
 * Time: 上午11:35
 * To change this template use File | Settings | File Templates.
 */
public class ImageUtil {

    public static Bitmap toGrayscale(Bitmap bmpOriginal) {
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();

        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
                Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        bmpOriginal.recycle();
        return bmpGrayscale;
    }
    
    public static Bitmap cutCircularImage(Bitmap bitmap) {
		try {
			Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
					bitmap.getHeight(), Config.ARGB_8888);
			Canvas canvas = new Canvas(output);
			final Paint paint = new Paint();
			final Rect rect = new Rect(0, 0, bitmap.getWidth(),
					bitmap.getHeight());
			final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
					bitmap.getHeight()));
			final float roundPx = bitmap.getWidth() / 2;
			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(Color.BLACK);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			final Rect src = new Rect(0, 0, bitmap.getWidth(),
					bitmap.getHeight());
			canvas.drawBitmap(bitmap, src, rect, paint);
			bitmap.recycle();
			return output;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}
    
    /**
	 * cut a rounded image
	 * 
	 * @param cornerSize
	 * @param bitmap
	 * @return
	 */
	public static Bitmap cutRoundedImage(int cornerSize, Bitmap bitmap) {
		try {
			// final int th = 32;
			// final int tp = 16;
			final Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			paint.setColor(Color.BLACK);
			Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
					bitmap.getHeight(), Config.ARGB_8888);
			final int bw = bitmap.getWidth();
			final int bh = bitmap.getHeight();
			Canvas canvas = new Canvas(output);
			final Rect des = new Rect(0, 0, bw, bh);
			final Rect src = new Rect(0, 0, bw, bh);
			canvas.drawARGB(0, 0, 0, 0);
			final RectF rectF = new RectF(new Rect(0, 0, (cornerSize << 1) + 8,
					bh));
			Path p = new Path();
			p.setFillType(Path.FillType.WINDING);
			final int rrx = cornerSize + 4;
			p.addRect(rrx, 0, bw, bh, Path.Direction.CW);
			p.addRoundRect(rectF, cornerSize, cornerSize, Path.Direction.CW);

			canvas.drawPath(p, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, src, des, paint);
			bitmap.recycle();
			return output;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}
}
